package com.ds.applet.service.impl;

import java.util.List;

import com.ds.applet.domain.MiniPledgeNum;
import com.ds.applet.mapper.MiniPledgeNumMapper;
import com.ds.common.core.domain.AjaxResult;
import com.ds.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.ds.applet.mapper.MiniQualityGuaranteeMapper;
import com.ds.applet.domain.MiniQualityGuarantee;
import com.ds.applet.service.IMiniQualityGuaranteeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 质保管理Service业务层处理
 *
 * @author liucan
 * @date 2024-03-28
 */
@Service
public class MiniQualityGuaranteeServiceImpl implements IMiniQualityGuaranteeService {
    @Resource
    private MiniQualityGuaranteeMapper miniQualityGuaranteeMapper;

    @Resource
    private MiniPledgeNumMapper miniPledgeNumMapper;

    /**
     * 查询质保管理
     *
     * @param id 质保管理主键
     * @return 质保管理
     */
    @Override
    public MiniQualityGuarantee selectMiniQualityGuaranteeById(Long id) {
        return miniQualityGuaranteeMapper.selectMiniQualityGuaranteeById(id);
    }

    /**
     * 查询质保管理列表
     *
     * @param miniQualityGuarantee 质保管理
     * @return 质保管理
     */
    @Override
    public List<MiniQualityGuarantee> selectMiniQualityGuaranteeList(MiniQualityGuarantee miniQualityGuarantee) {
        return miniQualityGuaranteeMapper.selectMiniQualityGuaranteeList(miniQualityGuarantee);
    }

    /**
     * 新增质保管理
     *
     * @param miniQualityGuarantee 质保管理
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult insertMiniQualityGuarantee(MiniQualityGuarantee miniQualityGuarantee) {
//        miniQualityGuarantee.setCreationTime(DateUtils.getTime());//创建时间
//        String quQualityNum = miniQualityGuarantee.getQuQualityNum();//质保编号
//        if (!quQualityNum.isEmpty()) {
//            //根据填写的质保编号和选择的施工店名查询质保编号表MiniPledgeNum是否有对应的未使用信息，有的话添加成功，没有的话返回质保编号不存在
//            MiniPledgeNum miniPledgeNum = new MiniPledgeNum();
//            miniPledgeNum.setCoNum(quQualityNum);//质保编号
//            miniPledgeNum.setCoGelten(0);//0未使用
//            miniPledgeNum.setCoShow(0);//0启用
//            miniPledgeNum.setLeId(miniQualityGuarantee.getQuShopName());//施工店（加盟商）
//            miniPledgeNum.setToApplet(miniQualityGuarantee.getToApplet());//所属小程序
//            int size = miniPledgeNumMapper.selectMiniPledgeNumList(miniPledgeNum).size();
//            if (size > 0) {
//                int i = miniQualityGuaranteeMapper.insertMiniQualityGuarantee(miniQualityGuarantee);
//                if (i > 0) {
//                    //i>0说明添加成功了，这个时候就要修改 MiniPledgeNum质保编码对象表 coGelten为1已使用
//                    MiniPledgeNum miniPledgeNum1 = new MiniPledgeNum();
//                    miniPledgeNum1.setCoNum(quQualityNum);//质保编码
//                    miniPledgeNum1.setLeId(miniQualityGuarantee.getQuShopName());//加盟商
//                    miniPledgeNum1.setCoGelten(1);//是否使用 1已使用
//                    i = miniPledgeNumMapper.updateMiniPledgeNumBy(miniPledgeNum1);
//                }
//                return AjaxResult.success(i);
//            }
//            return AjaxResult.error("该质保编号未分配！");
//        }
//        int i = miniQualityGuaranteeMapper.insertMiniQualityGuarantee(miniQualityGuarantee);
//        if (i > 0) {
//            //i>0说明添加成功了，这个时候就要修改 MiniPledgeNum质保编码对象表 coGelten为1已使用
//            MiniPledgeNum miniPledgeNum1 = new MiniPledgeNum();
//            miniPledgeNum1.setCoNum(quQualityNum);//质保编码
//            miniPledgeNum1.setLeId(miniQualityGuarantee.getQuShopName());//加盟商
//            miniPledgeNum1.setCoGelten(1);//是否使用 1已使用
//            i = miniPledgeNumMapper.updateMiniPledgeNumBy(miniPledgeNum1);
//        }
//        return AjaxResult.success(i);
        return AjaxResult.success(miniQualityGuaranteeMapper.insertMiniQualityGuarantee(miniQualityGuarantee));
    }

    /**
     * 修改质保管理
     *
     * @param miniQualityGuarantee 质保管理
     * @return 结果
     */
    @Override
    public int updateMiniQualityGuarantee(MiniQualityGuarantee miniQualityGuarantee) {
        return miniQualityGuaranteeMapper.updateMiniQualityGuarantee(miniQualityGuarantee);
    }

    /**
     * 批量删除质保管理
     *
     * @param ids 需要删除的质保管理主键
     * @return 结果
     */
    @Override
    public int deleteMiniQualityGuaranteeByIds(Long[] ids) {
        return miniQualityGuaranteeMapper.deleteMiniQualityGuaranteeByIds(ids);
    }

    /**
     * 删除质保管理信息
     *
     * @param id 质保管理主键
     * @return 结果
     */
    @Override
    public int deleteMiniQualityGuaranteeById(Long id) {
        return miniQualityGuaranteeMapper.deleteMiniQualityGuaranteeById(id);
    }

    /**
     * 根据手机号、质保卡号、车架号车牌号查询
     */
    @Override
    public List<MiniQualityGuarantee> select(MiniQualityGuarantee miniQualityGuarantee) {
        return miniQualityGuaranteeMapper.select(miniQualityGuarantee);
    }
}
